import java.util.Arrays;

public class Solution885 {
    public int[][] spiralMatrixIII(int rows, int cols, int rStart, int cStart) {
        int[] dx=new int[]{0,1,0,-1};
        int[] dy=new int[]{1,0,-1,0};
        int cur=1,step=0,x=rStart,y=cStart,d=0;
        int[][] ans=new int[rows*cols][2];
        ans[0][0]=rStart;
        ans[0][1]=cStart;
        while (cur<rows*cols){
            for (int i=0;i<step/2+1;i++){
                x=x+dx[d];
                y=y+dy[d];
                if (x>=0 && x<rows && y>=0 && y<cols){
                    ans[cur][0]=x;
                    ans[cur][1]=y;
                    cur++;
                }
            }
            step++;
            d=(d+1)%4;
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(Arrays.deepToString(new Solution885().spiralMatrixIII(1, 4, 0, 0)));
    }
}
